package site.yaoer.dongbao.ums.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class  JwtUtils {

    private static final String secret = "1231aadfasdplm.;";

    private static int tokenExpireTime;

    @Value("${site.yaoer.token.expire-time}")
    public void setTokenExpireTime(int tokenExpireTime) {
        JwtUtils.tokenExpireTime = tokenExpireTime;
    }

    public static String createToken(String subject) {
        return Jwts.builder().setSubject(subject).setExpiration(new Date(System.currentTimeMillis() + tokenExpireTime * 1000L * 60L)).signWith(SignatureAlgorithm.HS256, secret).compact();
    }

    public static String parseToken(String token) {
        return Jwts.parser().setSigningKey(secret).parseClaimsJws(token).getBody().getSubject();
    }

    public static void main(String[] args) {
        String token = createToken("{\"name\":\"张三\",\"age\":25}");
        System.out.println("Token :" + token);
        System.out.println("解析：" + parseToken(token));
    }
}
